<template>
	<view class="my">
		<view class="my-1">
			<image src="https://master.shengejiaju.cn/web/static/icons/pic_bg.png" mode="widthFix" class="bg"></image>
			<view class="my-tit">我的</view>
			<view class="my-1-info">
				<block v-if="userInfo.user_name && userInfo.user_name!=0">
					<image class="avatar" :src="userInfo.heard_img?userInfo.heard_img:'https://master.shengejiaju.cn/web/static/icons/shop-default.png'"></image>
					<view class="my-1-info-box">
						<view class="my-1-info-tit">{{userInfo.nickname}}</view>
						<view class="my-1-info-id">
							<text>ID:{{userInfo.user_id}}</text>
							<text>店铺编号：{{userInfo.store_id}}</text>
							<image src="https://master.shengejiaju.cn/web/static/icons/label@2x.png" v-if="userInfo.role_type==2"></image>
							<image src="https://master.shengejiaju.cn/web/static/icons/label@2x1.png" v-if="userInfo.role_type==4"></image>
							<image src="https://master.shengejiaju.cn/web/static/icons/label@2x2.png" v-if="userInfo.role_type==11"></image>
						</view>
					</view>
				</block>
				<block v-else>
					<!-- #ifdef MP-WEIXIN -->
					<view class="bindwx"><button type="primary" hover-class="none" size="mini" open-type="getUserInfo" @getuserinfo="bindwx">绑定微信</button></view>
					<!-- #endif -->
				</block>
			</view>
		</view>
		<!-- <navigator url="/pages/my/wallet/wallet">
			<view class="my-2">
				<view class="my-2-item">
					<image src="https://master.shengejiaju.cn/web/static/icons/line.png"></image>
					<view>{{money.history_money}}</view>
					<text>累计收益(元)</text>
				</view>
				<view class="my-2-item">
					<image src="https://master.shengejiaju.cn/web/static/icons/line.png"></image>
					<view>{{money.money}}</view>
					<text>余额</text>
				</view>
				<view class="my-2-item">
					<image src="https://master.shengejiaju.cn/web/static/icons/line.png"></image>
					<view>{{money.take_money}}</view>
					<text>待到账</text>
				</view>
			</view>
		</navigator> -->
		<!-- #ifdef MP-WEIXIN -->
		<view class="shop-enter" @click="goShop">
			<image src="https://master.shengejiaju.cn/web/static/icons/shop-enter.png" mode="widthFix"></image>
		</view>
		<!-- #endif -->
		<view class="my-3">
			<view class="my-3-item">
				<navigator url="/pages/my/store-settings">
					<image class="ico" src="https://master.shengejiaju.cn/web/static/icons/ico_setting@2x.png"></image>
					<text>店铺设置</text>
					<image class="arrow" src="https://master.shengejiaju.cn/web/static/icons/ico_skip_mine@2x.png"></image>
				</navigator>
			</view>
			<view class="my-3-item">
				<navigator url="/pages/my/wallet/wallet">
					<image class="ico" src="https://master.shengejiaju.cn/web/static/icons/ico_money@2x.png"></image>
					<text>我的钱包</text>
					<image class="arrow" src="https://master.shengejiaju.cn/web/static/icons/ico_skip_mine@2x.png"></image>
				</navigator>
			</view>
			<!-- <view class="my-3-item">
				<navigator url="/pages/my/contract">
					<image class="ico" src="https://master.shengejiaju.cn/web/static/icons/ico_contract@2x.png"></image>
					<text>我的合同</text>
					<image class="arrow" src="https://master.shengejiaju.cn/web/static/icons/ico_skip_mine@2x.png"></image>
				</navigator>
			</view> -->
			<!-- <view class="my-3-item">
				<navigator url="/pages/my/promote/promote">
					<image class="ico" src="https://master.shengejiaju.cn/web/static/icons/ico_tg@2x.png"></image>
					<text>我的推广</text>
					<image class="arrow" src="https://master.shengejiaju.cn/web/static/icons/ico_skip_mine@2x.png"></image>
				</navigator>
			</view> -->
			<view class="my-3-item">
				<navigator url="/pages/order/purchase/purchase">
					<image class="ico" src="https://master.shengejiaju.cn/web/static/icons/ico_order@2x.png"></image>
					<text>进货订单</text>
					<image class="arrow" src="https://master.shengejiaju.cn/web/static/icons/ico_skip_mine@2x.png"></image>
				</navigator>
			</view>
			<!-- <view class="my-3-item">
				<navigator @click="noContent">
					<image class="ico" src="https://master.shengejiaju.cn/web/static/icons/ico_handle@2x.png"></image>
					<text>售后处理</text>
					<image class="arrow" src="https://master.shengejiaju.cn/web/static/icons/ico_skip_mine@2x.png"></image>
				</navigator>
			</view> -->
			<view class="my-3-item">
				<navigator url="/pages/my/share">
					<image class="ico" src="https://master.shengejiaju.cn/web/static/icons/ico_share@2x.png"></image>
					<text>首页分享</text>
					<image class="arrow" src="https://master.shengejiaju.cn/web/static/icons/ico_skip_mine@2x.png"></image>
				</navigator>
			</view>
			<view class="my-3-item">
				<navigator url="/pages/my/poster-list/poster-list">
					<image class="ico" src="https://master.shengejiaju.cn/web/static/icons/ico_banner_mine@2x.png"></image>
					<text>我的海报</text>
					<image class="arrow" src="https://master.shengejiaju.cn/web/static/icons/ico_skip_mine@2x.png"></image>
				</navigator>
			</view>
			<!-- <view class="my-3-item">
				<navigator url="/pages/my/address/address">
					<image class="ico" src="https://master.shengejiaju.cn/web/static/icons/ico_addr@2x.png"></image>
					<text>商家地址</text>
					<image class="arrow" src="https://master.shengejiaju.cn/web/static/icons/ico_skip_mine@2x.png"></image>
				</navigator>
			</view>
			<view class="my-3-item">
				<navigator url="/pages/my/user-logistics/user-logistics">
					<image class="ico" src="https://master.shengejiaju.cn/web/static/icons/icon_logistics.png"></image>
					<text>商家物流</text>
					<image class="arrow" src="https://master.shengejiaju.cn/web/static/icons/ico_skip_mine@2x.png"></image>
				</navigator>
			</view> -->
			<!-- #ifdef APP-PLUS -->
			<view class="my-3-item">
				<navigator :url="'/pages/webview/webview?url='+ encodeURIComponent('https://master.jiaju01.com/web/merchant/#/pages/news/news-info?id=28')">
					<image class="ico" src="https://master.shengejiaju.cn/web/static/icons/protocol.png"></image>
					<text>用户协议和隐私政策</text>
					<image class="arrow" src="https://master.shengejiaju.cn/web/static/icons/ico_skip_mine@2x.png"></image>
				</navigator>
			</view>
			<!-- #endif -->
		</view>
		<view class="logout" @click="logout">退出登录</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				userInfo: {
					user_id: '',
					heard_img: '',
					nickname: '',
					store_id: '',
					store_logo: '',
					store_name: '',
					role_type: ''
				},
				money: {
					history_money: 0,
					money: 0,
					take_money: 0
				},
				code: ''
			}
		},
		onLoad() {
			// #ifdef MP-WEIXIN
			uni.login({
				provider: 'weixin',
				success: (loginRes) => {
					this.code = loginRes.code
				}
			})
			// #endif
		},
		onShow() {
			uni.showTabBar()
			this.getUserInfo()
			this.init()
		},
		methods: {
			getUserInfo(){
				this.$api.UserInfo().then(res => {
					this.userInfo = JSON.parse(uni.getStorageSync('userInfo'))
					if(this.userInfo.ios){
						this.userInfo.userInfo = 2
					}else{
						uni.setStorageSync('userInfo',JSON.stringify(res.datas))
						this.userInfo = {
							user_id: res.datas.user_id,
							heard_img: res.datas.heard_img,
							nickname: res.datas.nickname,
							store_id: res.datas.store_id,
							store_logo: res.datas.store_logo,
							store_name: res.datas.store_name,
							role_type: res.datas.role_type,
							user_name: res.datas.user_name
						}
					}
				})
			},
			noContent(){
				uni.showToast({
					icon: 'none',
					title: '此模块正在开发中···'
				})
			},
			init(){
				this.$api.GetStoreMoney({store_id: JSON.parse(uni.getStorageSync('userInfo')).store_id}).then(res => {
					this.money = {
						history_money: res.datas.history_money,
						money: res.datas.money,
						take_money: res.datas.take_money
					}
				})
			},
			logout(){
				uni.showModal({
					content: '是否退出当前账号？',
					success(res) {
						if(res.confirm){
							uni.clearStorageSync('userInfo')
							uni.showToast({
								icon: "none",
								title: '退出成功',
								duration: 1500
							})
							setTimeout(() => {
								uni.reLaunch({
									url: '/pages/login/login'
								})
							},1500)
						}
					}
				})
			},
			bindwx(e){
				let _this = this
				// #ifdef H5
				uni.showModal({
					title: '温馨提示',
					content: 'H5暂不支持绑定微信，请移步小程序或APP绑定',
					showCancel: false,
					confirmText: '确定',
					success:(res) => {
						if(res.confirm){
							uni.switchTab({
								url: '/pages/index/index'
							})
						}
					}
				})
				// #endif
				// #ifdef APP-PLUS
				uni.login({
					provider: 'weixin',
					success(res) {
						uni.getUserInfo({
							provider: 'weixin',
							success: (userInfo) => {
								let userInfoData = {
									nickname: userInfo.userInfo.nickName,
									headimgurl:  userInfo.userInfo.avatarUrl,
									openID: userInfo.userInfo.openID,
									gender: userInfo.userInfo.gender,
									city: userInfo.userInfo.city,
									province: userInfo.userInfo.province,
									country: userInfo.userInfo.country,
									unionId: userInfo.userInfo.unionId
								}
								let data = {
									code: res.code,
									type: 2,
									phone: JSON.parse(uni.getStorageSync('userInfo')).phone,
									userInfo: JSON.stringify(userInfoData)
								}
								_this.$api.NewAppBinding(data).then(res => {
									uni.showToast({
										icon: 'none',
										title: '绑定成功',
										duration: 1500
									})
									setTimeout(() => {
										uni.switchTab({
											url: '/pages/index/index'
										})
									},1500)
								})
							}
						})
					}
				})
				// #endif
				// #ifdef MP-WEIXIN
				uni.login({
					provider: 'weixin',
					success: (loginRes) => {
						uni.checkSession({
							success: () => {
								console.log(e)
								this.code = loginRes.code
								console.log(this.code)
								let data = {
									code: this.code,
									encryptedData: e.detail.encryptedData,
									iv: e.detail.iv,
									phone: JSON.parse(uni.getStorageSync("userInfo")).phone,
									client_type: 2
								}
								this.$api.WeChatWxLogin(data).then(res => {
									this.getUserInfo()
									uni.showToast({
										icon: 'none',
										title: '绑定成功',
										duration: 1500
									})
								})
							},
							fail: () => {
								uni.login({
									provider: 'weixin',
									success: (loginRes) => {
										this.code = loginRes.code
									}
								})
							}
						})
					}
				})
				// #endif
			},
			goShop(){
				uni.navigateToMiniProgram({
					appId: 'wxc87b8c5576af43eb'
				})
			}
		}
	}
</script>

<style lang="scss">
	@import '../../static/scss/my.scss';
	.shop-enter{
		text-align: center;
		padding: 20rpx 20rpx 0;
		image{
			width: 100%;
		}
	}
</style>